
/*========== 找回密码 ============*/
	var oResetBox = document.getElementById("reset-box");
	var oVertifyBox = document.getElementById("vertify-email");
	var oEmail = document.getElementById("email");
	var oSecurityCode = document.getElementById("security-code");
	var oGetSecurityCode = document.getElementById("get-security-code");
	var oSubmitEmail = document.getElementById("submit-email");

	var oResetPasswordBox = document.getElementById("reset-password");
	var oPassword = document.getElementById("password");
	var oSurePassword = document.getElementById("sure-password");
	var oSubmitPassword = document.getElementById("submit-password");

	var oProgress = document.getElementById("progress");
	var oEmailTip = document.getElementById("email-tip");
	var oPasswordTip = document.getElementById("password-tip");
	var oSureTip = document.getElementById("sure-tip");
	var oSecurityTip = document.getElementById("security-tip");
	var oResetTip = document.getElementById("reset-tip");

	var emailExist = 0;

	var oArrowLeft = document.getElementById("arrow-left");
	var oArrowRight = document.getElementById("arrow-right");
	oArrowLeft.onclick = function(){
		oArrowRight.style.display = "block";
		vertifyBoxShow();
	}
	oArrowRight.onclick = function(){
		resetPasswordBoxShow();
	}


	oEmail.focus();
	oEmail.onkeyup = function(){checkEmail(oEmail,oEmailTip);};
	oEmail.onblur = function(){checkEmail(oEmail,oEmailTip);};
	oSecurityCode.onkeyup = function(){checkSecurityCode(oSecurityCode,oSecurityTip);};

	oSubmitEmail.onclick = function(){
		if(emailExist == 1){
			if(checkSecurityCode(oSecurityCode,oSecurityTip) && checkEmail(oEmail,oEmailTip)){
				ajax({
					type : "post",
					url : "/email_captcha_check/",
					data : {
						"email" : oEmail.value,
						"security-code" : oSecurityCode.value
					},
					success : EmailSubmitSuccess,
					error : submitError
				});
			}
		}else if(emailExist == -1){
			oEmailTip.style.color = "#f63";
			oEmailTip.innerHTML = "该用户邮箱不存在";
		}else{
			oSecurityTip.style.color = "#f63";
			oSecurityTip.innerHTML = "别急，还没获取验证码呢";
		}
	}

	function EmailSubmitSuccess(response){
	    var respJson = JSON.parse(response);
	    console.log(response);
		if(respJson.check == 1){
		    oArrowLeft.style.display = "block";
            resetPasswordBoxShow();
            oPassword.onkeyup = function(){checkPassword(oPassword,oPasswordTip,oSurePassword,oSureTip);};
            oSurePassword.onkeyup = function(){checkSurePassword(oPassword,oSurePassword,oSureTip);};
            oSubmitPassword.onclick = function(){
                if(checkPassword(oPassword,oPasswordTip,oSurePassword,oSureTip)&&checkSurePassword(oPassword,oSurePassword,oSureTip)){
                    ajax({
                        type : "post",
                        url : "/pwd_reset/",
                        data : {
                            "email" : oEmail.value,
                            "password" : oPassword.value
                        },
                        success : passwordSubmitSuccess,
                        error : submitError
                    });
                }
            }

		}else if(respJson.check == -1){
			oResetTip.innerHTML = "验证码错误，请重试";
			resetBtn();
		}
	}
	function passwordSubmitSuccess(response){
		var respJson = JSON.parse(response);
		if(respJson.status == 1){
			resetBtn();
			oResetBox.innerHTML = "<h1>找回密码</h1><p>修改密码成功√<br><a href='http://127.0.0.1:8000/'>跳回主页登录</a></p>";
		}else if(respJson.status == -1){
			oResetTip.innerHTML = "非常抱歉，服务器发生错误";
			resetBtn();
		}
	}
	function submitError(){
		oResetTip.innerHTML = "访问服务器失败";
	}
	function vertifyBoxShow(){
		oVertifyBox.style.display = "block";
		oResetPasswordBox.style.display = "none";
		oProgress.innerHTML = "@验证邮箱"
	}
	function resetPasswordBoxShow(){
		oVertifyBox.style.display = "none";
		oResetPasswordBox.style.display = "block";
		oProgress.innerHTML = "@您已通过验证，请重新设置密码"
	}
	/*===============  获取验证码  ===============*/

	oGetSecurityCode.onclick = askSecurityCode;
	var securityCodetimer;
	var time = 60;
	function askSecurityCode(){
		if(checkEmail(oEmail,oEmailTip)){
			oGetSecurityCode.onclick = null;
			securityCodetimer = window.setInterval(setTime,1000);
			ajax({
				type : "post",
				url : "/email_captcha/",
				data : {
				    "ask_type": 2,
					"email" : oEmail.value
				},
				success : askSecurityCodeSuccess,
				error : askSecurityCodeError
			});
		}
	}
	function askSecurityCodeSuccess(response){
		var responseJson = JSON.parse(response);
		if(responseJson.email == 0){
			oEmailTip.style.color = "#f63";
			oEmailTip.innerHTML = "该用户邮箱不存在";
			emailExist = -1;
			resetBtn();
		}else if(responseJson.email == 1){
			emailExist = 1;
			oSecurityTip.style.color = "#0c0";
			oSecurityTip.innerHTML = "获取验证码成功，快去邮箱查看吧";
		}else if(responseJson.email == -1){
			oResetTip.innerHTML = "非常抱歉，服务器发生错误";
			resetBtn();
		}
	}
	function askSecurityCodeError(){
		oResetTip.innerHTML = "访问服务器失败";
	}
	function setTime(){
		oGetSecurityCode.innerHTML = time + "s";
		time--;
		if( time == -1 ){
			resetBtn();
		}
	}
	function resetBtn(){
		window.clearInterval(securityCodetimer);
		time = 60;
		oGetSecurityCode.innerHTML = "重新获取";
		oGetSecurityCode.onclick = askSecurityCode;
	}